package com.wcx.imitationwechatbilllist;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.wcx.imitationwechatbilllist.details.ListDetailsActivity;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: 魏
 * @date: 2025/9/29
 * 账单适配器实现
 */
public class BillAdapter extends BaseQuickAdapter<BillItem, BaseViewHolder>
        implements StickyHeaderInterface {

    private final Context mContext;
    public BillAdapter(Context mContext, List<BillItem> data) {
        super(R.layout.item_bill, data);
        this.mContext = mContext;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, BillItem item) {
        LinearLayout llt_header = holder.findView(R.id.llt_header);
        TextView tv_month = holder.findView(R.id.tv_month);
        RecyclerView recycler_view = holder.findView(R.id.recycler_view);

        if (item.isHeader){
            llt_header.setVisibility(View.VISIBLE);
        }else {
            llt_header.setVisibility(View.GONE);
        }
        tv_month.setText(item.getMonth());
        // 设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recycler_view.setLayoutManager(layoutManager);

        ItemBillAdapter itemBillAdapter = new ItemBillAdapter(mContext, R.layout.item_item_bill, item.getList());
        recycler_view.setAdapter(itemBillAdapter);

        itemBillAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent(mContext, ListDetailsActivity.class);
                mContext.startActivity(intent);
            }
        });


    }

    // StickyHeaderInterface 实现
    @Override
    public boolean isHeader(int position) {
        return getItem(position).isHeader();
    }

    @Override
    public int getHeaderPositionForItem(int itemPosition) {
        for (int i = itemPosition; i >= 0; i--) {
            if (isHeader(i)) {
                return i;
            }
        }
        return RecyclerView.NO_POSITION;
    }

    @Override
    public View getHeaderView(RecyclerView parent, int headerPosition) {
        View headerView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_header, parent, false);
        TextView tvMonth = headerView.findViewById(R.id.tv_month);
        tvMonth.setText(getItem(headerPosition).getMonth());
        return headerView;
    }

    @Override
    public void onHeaderClick(int position) {
        Toast.makeText(mContext, "点击了头部：" + getItem(position).getMonth(),
                Toast.LENGTH_SHORT).show();
    }
}